<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

use Admin\Controller\AdminController;

use Admin\Model\GoodsModel;
use Admin\Model\GoodsTypeModel;
use Admin\Model\GoodsTagsModel;
/**
 * @author wangnaixin
 * @todo 产品模块
 * @menu 商城中心
 * @author wangnaixin
 * @menuAuto index
 * @order 210
 * @icon article.png
 */
class GoodsController extends AdminController {
    //put your code here
    
    private $goods;
    private $goodsType;
    private $goodsTags;
    
    function __construct() {
        parent::__construct();
        $this->goods = new GoodsModel();
        $this->goodsType = new GoodsTypeModel();
        $this->goodsTags = new GoodsTagsModel();
    }
    
    
    public function index(){
        $this->display();
    }
    
    /**
     * @todo 产品列表
     * @menu 产品列表
     * @order 1
     */
    public function goodsList(){
        $this->setForm();
        $goods_name = I('goods_name');
        if(!empty($goods_name)){
            $where['goods_name'] = ['like',"%$goods_name%"];
        }
        $goods_type = I('goods_type');
        if(!empty($goods_type)){
            $where['goods_type'] = ['eq',$goods_type];
        }
		$goods_tags = I('goods_tags');
        if(!empty($goods_tags)){
            $where['goods_tags'] = ['like',"%$goods_tags%"];
        }
        $list = $this->goods->getGoodsList($where);
		//dump($this->goods->getLastSql());die();
		$tagList= $this->goodsTags->getList();
        $this->assign("list",$list);
		$this->assign("tagList",$tagList);
        $this->assign("cateList",$this->goodsType->getList());
        //dump($list);die();
        $this->display();
    }
    /**
     * @todo 产品分类
     * @menu 产品分类
     * @order 20
     */
    public function goodsType(){
        $this->assign("cateList",$this->goodsType->getList());
        $this->display();
    }
    
    /**
     * @todo 产品标签
     * @menu 标签列表
     * @order 40
     */
    public function goodsTags(){
        $this->assign("list",$this->goodsTags->getList());
        $this->display();
    }
    
    public function addGoods(){
        $this->assign("cateList",$this->goodsType->getList());
        $this->assign("tagsList",$this->goodsTags->getList());
        $this->assign("cy_goods_type",C('CY_GOODS_TYPE'));
        $this->assign("cy_goods_type_json",json_encode(C('CY_GOODS_TYPE')));
		//dump(C('CY_GOODS_TYPE'));die();
        $this->display();
    }
    
    public function doAddGoods(){
        if ($data = $this->goods->create()) {
            $goodsId = I('id');
            unset($data['id']);
            //处理goodsTags 数据
            $data['goods_tags'] = implode("|", I('tag'));
            if(empty($goodsId)){
            	if ($this->goods->add($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品添加成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品添加失败'));
                }
            }else{
            	if ($this->goods->where("id = ".$goodsId)->save($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品修改成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品修改失败'));
                }
            }
            
        } else {
            $this->ajaxReturn(array("status"=>true,'msg'=>'产品添加失败'));
        }
    }
    
    public function editGoods(){
        $goodsId = I('goodsId');
        $goodsInfo = $this->goods->getGoodsInfoById($goodsId);
        $this->assign("goodsInfo",$goodsInfo);
        $cateList = $this->goodsType->getList();
//        dump($goodsInfo);die();
        $this->assign("cateList", $cateList);
        $this->assign("tagsList",$this->goodsTags->getList());
        $this->assign("cy_goods_type",C('CY_GOODS_TYPE'));
        $this->assign("cy_goods_type_json",json_encode(C('CY_GOODS_TYPE')));
        $this->display("addGoods");
    }

    
    public function addGoodsType(){
        $cateList = $this->goodsType->getList(array('pid' => 0));
        $this->assign("cateList", $cateList);
        $this->display();
    }
    
    public function doAddGoodsTags(){
        if ($data = $this->goodsTags->create()) {
            $cateId = I('id');
            if(empty($cateId)){
                unset($data['id']);
                if ($this->goodsTags->add($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品标签添加成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品标签添加失败'));
                }
            }else{
                if ($this->goodsTags->where("id = ".$cateId)->save()) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品标签修改成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品标签修改失败'));
                }
            }
            
        } else {
            $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类添加失败'));
        }
    }
    
    public function doAddGoodsCate(){
        if ($data = $this->goodsType->create()) {
            $cateId = I('id');
            if(empty($cateId)){
				unset($data['id']);
                if ($this->goodsType->add($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类添加成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类添加失败'));
                }
            }else{
                if ($this->goodsType->where("id = ".$cateId)->save()) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类修改成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类修改失败'));
                }
            }
            
        } else {
            $this->ajaxReturn(array("status"=>true,'msg'=>'产品分类添加失败'));
        }
    }
    
    public function editGoodsTags(){
        $cateId = I('cateId');
        $cateInfo = $this->goodsTags->getTagsInfoById($cateId);
        $this->assign("cateInfo",$cateInfo);
        $this->display("addGoodsTags");
    }
    
    public function delGoodsTags(){
        $cateId = I('cateId');
        $this->goodsTags->where("id=".$cateId)->delete();
        $this->ajaxReturn(true);
    }
    
    public function editGoodsType(){
        $cateId = I('cateId');
        $cateInfo = $this->goodsType->getTypeInfoById($cateId);
        $this->assign("cateInfo",$cateInfo);
        $cateList = $this->goodsType->getList(array('pid' => 0));
        $this->assign("cateList", $cateList);
        $this->display("addGoodsType");
    }
    
    public function delGoodsCate(){
        $cateId = I('cateId');
        $this->goodsType->delTypeById($cateId);
        $this->ajaxReturn(true);
    }
    
    public function delGoods(){
        $goodsId = I('goodsId');
        $this->goods->where("id=".$goodsId)->delete();
        $this->ajaxReturn(true);
    }
    
    public function setSpec(){
        $goodsId = I('goodsId');
        $goodsInfo = $this->goods->getGoodsInfoById($goodsId);
        //dump($goodsInfo);die();
        $this->assign("goodsInfo",$goodsInfo);
        $this->display();
    }
    
    public function doAddGoodsSpec(){
        //$this->ajaxReturn($_REQUEST);
        $spec_1 = I('spec_1');
        $spec_2 = I('spec_2');
        $spec_img = I('spec_img');
        $spec_integral = I('spec_integral');
        $spec_price = I('spec_price');
        $stock = I('stock');
        $goods_id = I('id');
        $spec_id = I('spec_id');
        $saveData = [];
        foreach($spec_1 as $k=>$v){
            $sData = [
                'spec_1' => $v,
                'spec_2' => $spec_2[$k],
                'spec_img' => $spec_img[$k],
                'spec_integral' =>$spec_integral[$k],
                'spec_price' => $spec_price[$k],
                'stock' => $stock[$k],
                'ctime' => time(),
                'goods_id' => $goods_id
            ];
            if(!empty($spec_id[$k])){
                M('goods_spec')->where("id=".$spec_id[$k])->save($sData);
            }else{
                if(!empty($sData['spec_1']) && !empty($sData['spec_price']) && !empty($sData['spec_integral']) && !empty($sData['stock']) && !empty($sData['goods_id']) ){
                    $saveData[] = $sData;
                }
                
            }
        }
        M('goods_spec')->addAll($saveData);
        $this->ajaxReturn(true);
    }
}
